package com.ra.landlord;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.espirit.eap.ICheckModelDuplicateInfo;
import com.espirit.eap.sa.LoginUser;
import com.ra.system.Version;

/**
 * 
 * 租客信息表, 参见HouseOwnerUser
 * 
 * @author Jerry
 *
 */
@Entity
@Table(name = "ra_tenantuser")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class TenantUser implements Version, LandlordOwner, ICheckModelDuplicateInfo {
	
	private final static String[] UNIQUE_PROPERTIES = new String[] {"phone"};

	private Integer id;

	// 0 - 未知， 1 - 男， 2 - 女
	private Integer gender;
	
	private String password;
	//头像ID
	private Integer pictureId;
	
	//头像文件名
	private String photoFileName;
	
	//@Transient
	private String photoContentType;
	
	//@Transient
	private File photo;
	
	private String name;
	
	// 用户身份证号
	private String idCard;
	
	// 用户昵称
	private String aliase;
	
	private String email;
	
	private String phone;
	
	private String phoneTwo;
	
	// 证件类型
	// 0 - 居住证， 1 - 学生证， 2 - 其它
	private Integer certificateType;
	
	// 证件号 (与idCard有重复的可能)
	private String certificate;
	
	private String qq;
	
	private String address;
	
	private Date birthday;
	
	private Date register;
	
	// 备注
	private String remark;
	//创建该用户的房东
	private LandlordOwnerUser landlordUser;
	
	private LoginUser loginUser;
	
	private List<Agreement> agreements;
	
	// 是否有微信账号(冗余字段，微信用户关注及取消关注时，更新该字段值)
	private Boolean relatedWeixin;
		
	private Integer version;
	
	private Integer status;
	
	@Id
	@GeneratedValue
	@Column(name = "user_id")
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "gender")
	public Integer getGender() {
		return gender;
	}

	public void setGender(Integer gender) {
		this.gender = gender;
	}

	@Column(name = "password")
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Column(name = "user_pictures_id")
	public Integer getPictureId() {
		return pictureId;
	}

	public void setPictureId(Integer pictureId) {
		this.pictureId = pictureId;
	}

	@Column(name = "user_name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "user_id_card")
	public String getIdCardWithId() {
		return idCard;
	}

	public void setIdCardWithId(String idCard) {
		this.idCard = idCard;
	}
	
	@Transient
	public String getIdCard() {
		return idCard == null || !idCard.startsWith("ID") ? idCard : idCard.substring(2);
	}

	public void setIdCard(String idCard) {
		this.idCard = idCard == null ? null : "ID" + idCard;
	}

	@Column(name = "user_aliase")
	public String getAliase() {
		return aliase;
	}

	public void setAliase(String aliase) {
		this.aliase = aliase;
	}

	@Column(name = "user_e_mail")
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Column(name = "user_phone")
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	@Column(name = "user_telephone")
	public String getPhoneTwo() {
		return phoneTwo;
	}

	public void setPhoneTwo(String phoneTwo) {
		this.phoneTwo = phoneTwo;
	}

	@Column(name = "certificate_id")
	public Integer getCertificateType() {
		return certificateType;
	}

	public void setCertificateType(Integer certificateType) {
		this.certificateType = certificateType;
	}

	@Column(name = "user_certificate")
	public String getCertificate() {
		return certificate;
	}

	public void setCertificate(String certificate) {
		this.certificate = certificate;
	}

	@Column(name = "user_qq")
	public String getQq() {
		return qq;
	}

	public void setQq(String qq) {
		this.qq = qq;
	}

	@Column(name = "user_address")
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@Column(name = "user_birthday")
	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	@Column(name = "user_Registered")
	public Date getRegister() {
		return register;
	}

	public void setRegister(Date register) {
		this.register = register;
	}

	@Column(name = "user_note")
	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name = "createuserid")
	public LandlordOwnerUser getLandlordUser() {
		return landlordUser;
	}

	public void setLandlordUser(LandlordOwnerUser landlordUser) {
		this.landlordUser = landlordUser;
	}
	
	@Transient
	//@OneToOne(targetEntity=ValidAgreement.class, mappedBy="tenantUser", fetch=FetchType.LAZY, optional=true)
	public List<ValidAgreement> getValidAgreements() {
		List<ValidAgreement> validAgreements = new ArrayList<>();
		List<Agreement> lstagreement = getAgreements();
		if (lstagreement != null) {
			for (Agreement agreement : lstagreement) {
				if (agreement instanceof ValidAgreement) {
					validAgreements.add((ValidAgreement) agreement);
				}
			}
		}
		return validAgreements;
	}
	
	@OneToMany(targetEntity=Agreement.class, mappedBy="tenantUser", fetch=FetchType.LAZY)
	public List<Agreement> getAgreements() {
		return agreements;
	}

	public void setAgreements(List<Agreement> agreements) {
		this.agreements = agreements;
	}

	@ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.MERGE)
	@JoinColumn(name = "loginuser_id")
	public LoginUser getLoginUser() {
		return loginUser;
	}

	public void setLoginUser(LoginUser loginUser) {
		this.loginUser = loginUser;
	}
	
	@Column(name = "version_id")
	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	@Column(name = "data_status")
	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}
	
	@Column(name = "photo_file_name")
	public String getPhotoFileName() {
		return photoFileName;
	}

	public void setPhotoFileName(String photoFileName) {
		this.photoFileName = photoFileName;
	}
	
	public Boolean getRelatedWeixin() {
		return relatedWeixin;
	}

	public void setRelatedWeixin(Boolean relatedWeixin) {
		this.relatedWeixin = relatedWeixin;
	}

	@Transient
	public String getPhotoContentType() {
		return photoContentType;
	}

	public void setPhotoContentType(String photoContentType) {
		this.photoContentType = photoContentType;
	}

	@Transient
	public File getPhoto() {
		return photo;
	}

	public void setPhoto(File photo) {
		this.photo = photo;
	}
	
	@Override
	public String[] gatherProperties() {
		return UNIQUE_PROPERTIES;
	}	
}
